package com.kingdee.eas.hr.train.app;

import java.util.HashMap;
import java.util.Map;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.JobFactory;
import com.kingdee.eas.basedata.org.JobInfo;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.IPerson;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.emp.PersonDegreeCollection;
import com.kingdee.eas.hr.emp.PersonDegreeFactory;
import com.kingdee.eas.hr.emp.PersonPositionCollection;
import com.kingdee.eas.hr.emp.PersonPositionFactory;
import com.kingdee.eas.hr.emp.PersonTechnicalPostCollection;
import com.kingdee.eas.hr.emp.PersonTechnicalPostFactory;
import com.kingdee.eas.hr.train.TrainCommonUtils;
import com.kingdee.eas.hr.train.TrainConstants;

public class TrainFacadeControllerBeanEx extends TrainFacadeControllerBean {

	@Override
	protected Map _getEmployeeRelationInfo(Context ctx, String employeeId)
			throws BOSException, EASBizException {
		Map infos = new HashMap();
		if (TrainCommonUtils.isEmpty(employeeId)) {
			return infos;
		}

		IPerson iPerson = PersonFactory.getLocalInstance(ctx);
		PersonInfo personInfo = iPerson.getPersonInfo(new ObjectUuidPK(
				employeeId));
		PositionInfo posInfo = iPerson.getPrimaryPosition(personInfo.getId());

		infos.put("person", personInfo);
		infos.put(TrainConstants.NUMBER, personInfo.getNumber());

		if (personInfo.getGender() != null) {
			infos.put("sex", personInfo.getGender().getAlias());
		}

		if (posInfo != null) {
			infos.put(TrainConstants.POSITION, posInfo.getName());

			if (posInfo.getJob() != null) {
				JobInfo jobInfo = JobFactory.getLocalInstance(ctx).getJobInfo(
						new ObjectUuidPK(posInfo.getJob().getId().toString()));
				infos.put("job", jobInfo.getName());
			}

		}

		String sql = "select technicalPost.name where person.id = '"
				+ employeeId + "' and isHighTechnical = 1";
		PersonTechnicalPostCollection personPostCol = PersonTechnicalPostFactory
				.getLocalInstance(ctx).getPersonTechnicalPostCollection(sql);
		if (personPostCol.size() > 0) {
			infos.put("technicalPost", personPostCol.get(0).getTechnicalPost()
					.getName());
		}

		sql = "select joinGroupDate, jobGrade.name, adjustGroupVal,personDep.id,personDep.name where person.id = '"
				+ employeeId + "'";
		PersonPositionCollection personPositionCollection = PersonPositionFactory
				.getLocalInstance(ctx).getPersonPositionCollection(sql);
		if ((personPositionCollection.size() > 0)
				&& (personPositionCollection.get(0).getJobGrade() != null)) {
			infos.put("grade", personPositionCollection.get(0).getJobGrade()
					.getName());
			infos.put("joinGroupDate", personPositionCollection.get(0)
					.getJoinGroupDate());
			infos.put("adjustGroupVal", new Float(personPositionCollection.get(
					0).getAdjustGroupVal()));
		}

		if ((personPositionCollection.size() > 0)
				&& (personPositionCollection.get(0).getPersonDep() != null)) {
			AdminOrgUnitInfo unitInfo = personPositionCollection.get(0)
					.getPersonDep();
			infos.put("adminOrgUnit", unitInfo);
		}

		sql = "select diploma.name,specialty.id, specialty.name,where person.id = '"
				+ employeeId + "' and isHighest = 1";

		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add(new SelectorItemInfo("diploma.name"));
		selectors.add(new SelectorItemInfo("specialty.id"));
		selectors.add(new SelectorItemInfo("specialty.name"));

		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(new FilterItemInfo("person.id", employeeId));
		fi.getFilterItems().add(new FilterItemInfo("isHighest", 1));
		fi.setMaskString("#0 and #1");

		EntityViewInfo view = new EntityViewInfo();
		view.setSelector(selectors);
		view.setFilter(fi);

		PersonDegreeCollection personDegreeCol = PersonDegreeFactory
				.getLocalInstance(ctx).getPersonDegreeCollection(view);
		if ((personDegreeCol.size() > 0)
				&& (personDegreeCol.get(0).getDiploma() != null)) {
			infos.put("diploma", personDegreeCol.get(0).getDiploma().getName());

			if (personDegreeCol.get(0).getSpecialty() != null) {
				infos.put("special", personDegreeCol.get(0).getSpecialty()
						.getName());
			}
		}

		return infos;
	}

}
